package cc.openhome;

import java.io.*;
import java.net.URL;
import java.nio.ByteBuffer;
import java.mio.channels.*;

public class NIOUtil {
    public static void dump(ReadbleByteChannel src, WritableByteChannel dest) throws IOException{
        ByteBuffer buffer =  ByteBuffer.allocate(1024);
        try (ReadbleByteChannel srcCH = src;WritableByteChannel destCH = dest) {
            while (srcCH.read(buffer) != -1) {
                buffer.flip();
                destCH.write(buffer);
                buffer.clear();
            }
        }
    }

    public static void main(String[] args) {
        URL url = new URL("http://openhome.cc");
        ReadbleByteChannel src = Channels.newChannel(url.openStream());
        WritableByteChannel dest = new FileOutputSream("index.html").getChannel();
        NIOUtil.dump(src, dest);
    }

}
